package com.example.templateapp.ui.popupmenu

import android.animation.ObjectAnimator
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.BounceInterpolator
import android.view.animation.RotateAnimation
import android.widget.ImageView
import android.widget.PopupWindow
import com.example.templateapp.R
import com.example.templateapp.databinding.ActivityPopupMenuBinding
import com.example.templateapp.ui.base.BaseAppCompatActivity
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.activity_popup_menu.*

class PopupMenuActivity : BaseAppCompatActivity() {
    private val binding: ActivityPopupMenuBinding by binding(R.layout.activity_popup_menu)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val mContentView = LayoutInflater.from(this@PopupMenuActivity)
            .inflate(R.layout.window_popup_menu, null)
        val btn1 = mContentView.findViewById<ImageView>(R.id.btn1)
        val btn2 = mContentView.findViewById<ImageView>(R.id.btn2)
        val btn3 = mContentView.findViewById<ImageView>(R.id.btn3)
        val popupWindow = PopupWindow(this).apply {
            contentView = mContentView
            width = ViewGroup.LayoutParams.MATCH_PARENT
            height = ViewGroup.LayoutParams.MATCH_PARENT
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            isOutsideTouchable = true


            val rotateAnimation = RotateAnimation(
                45f,
                0f,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f
            ).apply {
                duration = 300
                fillAfter = true
            }

            btn1.setOnClickListener { ToastUtils.toast("btn1") }

            setOnDismissListener {
                setBackAlpha(false)
                binding.imageView.startAnimation(rotateAnimation)
                binding.imageView.setImageResource(R.drawable.bg_circle_btn)
            }

            mContentView.setOnClickListener { this.dismiss() }
        }

        binding.imageView.setOnClickListener {
            if (popupWindow.isShowing) {
                popupWindow.dismiss()
                setBackAlpha(false)
                return@setOnClickListener
            }
            setBackAlpha(true)
            imageView.setImageResource(R.drawable.bg_circle_btn_pressed)
            it.startAnimation(
                RotateAnimation(
                    0f,
                    45f,
                    Animation.RELATIVE_TO_SELF,
                    0.5f,
                    Animation.RELATIVE_TO_SELF,
                    0.5f
                ).apply {
                    duration = 300
                    fillAfter = true
                })
            popupWindow.showAsDropDown(it, 0, -135, Gravity.CENTER)

            animY(btn2)
            animY(btn1)
            ObjectAnimator.ofFloat(btn1, "translationX", 0f, -200f)
                .initAndStart()
            animY(btn3)
            ObjectAnimator.ofFloat(btn3, "translationX", 0f, 200f)
                .initAndStart()

        }
    }

    private fun animY(btn: ImageView?) {
        ObjectAnimator.ofFloat(btn, "translationY", 0f, -200f)
            .initAndStart()
    }

    private fun ObjectAnimator.initAndStart() {
        duration = 700
        interpolator = BounceInterpolator()
        start()
    }

    private fun setBackAlpha(isDark: Boolean) {
        val lp = window.attributes
        lp.alpha = if (isDark) 0.5f else 1f
        window.attributes = lp
    }
}